package org.nnxy.foodboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.nnxy.foodboot.entity.Recipe;
import org.nnxy.foodboot.entity.UserCollection;
import org.nnxy.foodboot.mapper.UserCollectionMapper;
import org.nnxy.foodboot.service.PopularRecipeService;
import org.nnxy.foodboot.service.UserCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户收藏服务实现类
 */
@Service
public class UserCollectionServiceImpl extends ServiceImpl<UserCollectionMapper, UserCollection> implements UserCollectionService {

    @Autowired
    private UserCollectionMapper userCollectionMapper;
    
    @Autowired
    private PopularRecipeService popularRecipeService;
    
    /**
     * 添加收藏
     *
     * @param userId 用户ID
     * @param recipeId 食谱ID
     * @param notes 收藏备注
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addCollection(Long userId, Long recipeId, String notes) {
        // 先检查是否已收藏
        if (hasCollected(userId, recipeId)) {
            return false;
        }
        
        // 创建收藏记录
        UserCollection collection = new UserCollection();
        collection.setUserId(userId);
        collection.setRecipeId(recipeId);
        collection.setNotes(notes);
        
        boolean result = this.save(collection);
        
        // 更新热门食谱指标
        if (result) {
            popularRecipeService.updateRecipePopularityMetrics(recipeId, 0, 0, 1);
        }
        
        return result;
    }
    
    /**
     * 取消收藏
     *
     * @param userId 用户ID
     * @param recipeId 食谱ID
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeCollection(Long userId, Long recipeId) {
        int count = (int) this.count(
                new LambdaQueryWrapper<UserCollection>()
                    .eq(UserCollection::getUserId, userId)
                    .eq(UserCollection::getRecipeId, recipeId)
        );
        
        if (count == 0) {
            return false;
        }
        
        boolean result = this.remove(
                new LambdaQueryWrapper<UserCollection>()
                    .eq(UserCollection::getUserId, userId)
                    .eq(UserCollection::getRecipeId, recipeId)
        );
        
        // 更新热门食谱指标
        if (result) {
            // 这里减1操作需要在热门食谱表中实现，这里简单处理
            popularRecipeService.updateRecipePopularityMetrics(recipeId, 0, 0, -1);
        }
        
        return result;
    }
    
    /**
     * 分页查询用户收藏
     *
     * @param page 分页参数
     * @param userId 用户ID
     * @return 分页结果
     */
    @Override
    public IPage<Recipe> pageUserCollections(Page<Recipe> page, Long userId) {
        return userCollectionMapper.selectUserCollectionRecipes(page, userId);
    }
    
    /**
     * 检查用户是否已收藏
     *
     * @param userId 用户ID
     * @param recipeId 食谱ID
     * @return 是否已收藏
     */
    @Override
    public boolean hasCollected(Long userId, Long recipeId) {
        Integer count = userCollectionMapper.checkUserCollection(userId, recipeId);
        return count != null && count > 0;
    }
} 